import { StepperDefaultProps } from './props';
import { getPrecision, getValidNumber } from './utils';
import fmtEvent from '../_util/fmtEvent';
import mixinValue from '../mixins/value';
Component({
    props: StepperDefaultProps,
    mixins: [mixinValue({
            transformValue: function (num, extra, precision) {
                var _a = getValidNumber(num, this.props.min, this.props.max, this.props.step, precision >= 0 ? precision : this.props.precision), valid = _a.valid, value = _a.value;
                if (valid && this.getValue() !== value) {
                    return {
                        needUpdate: true,
                        value: value,
                    };
                }
                return {
                    needUpdate: false,
                };
            },
        })],
    methods: {
        onFocus: function (e) {
            if (this.props.onFocus) {
                var value = this.getValue();
                this.props.onFocus(value === '' ? null : Number(value), fmtEvent(this.props, e));
            }
        },
        onChange: function (val, e) {
            var _a = this.update(val), needUpdate = _a.needUpdate, value = _a.value;
            if (this.props.onChange && needUpdate) {
                this.props.onChange(value === '' ? null : Number(value), fmtEvent(this.props, e));
            }
        },
        onConfirm: function (val, e) {
            if (this.props.onConfirm) {
                var value = this.getValue();
                this.props.onConfirm(value === '' ? null : Number(value), fmtEvent(this.props, e));
            }
        },
        onBlur: function (e) {
            if (this.isControlled()) {
                this.update(this.props.value);
            }
            if (this.props.onBlur) {
                var value = this.getValue();
                this.props.onBlur(value === '' ? null : Number(value), fmtEvent(this.props, e));
            }
        },
        onTap: function (e) {
            var _a = this.props, step = _a.step, disabled = _a.disabled, _b = _a.min, min = _b === void 0 ? -Infinity : _b, _c = _a.max, max = _c === void 0 ? Infinity : _c;
            var value = Number(this.getValue());
            if (!disabled) {
                var mode = e.currentTarget.dataset.mode;
                var result = value;
                var precision = this.props.precision >= 0 ? this.props.precision : Math.max(getPrecision(value), getPrecision(step));
                if (mode === 'minus') {
                    // 【减】按钮的操作
                    result = value - step;
                    if (result < min) {
                        result = min;
                    }
                }
                else if (mode === 'add') {
                    // 【加】按钮的操作
                    result = value + step;
                    if (result > max) {
                        result = max;
                    }
                }
                if (!this.isControlled()) {
                    var needUpdate = this.update(result, {}, precision).needUpdate;
                    if (!needUpdate) {
                        return;
                    }
                }
                if (this.props.onChange) {
                    var value_1 = getValidNumber(result, min, max, step, precision).value;
                    this.props.onChange(Number(value_1), fmtEvent(this.props, e));
                }
            }
        },
    },
});
